import xml.etree.ElementTree as ET
import redis

pool = redis.ConnectionPool(host='127.0.0.1', port=6379)
r = redis.Redis(connection_pool=pool)
r.flushall()  # 清空Redis
tree  = ET.parse('concentrate.xml')
root  = tree.getroot()
cutpre = '"_blank"'
cutend = '<'


for doc in root:
    doi = 0
    print(doc.tag)
    for field in doc.findall('field'):
        if(field.get('name') == 'doi'):
            doi = field.text
            print(doi)
        if(field.get('name') == 'ref'):
            str = field.text
            print(str)
            if(str.find(cutpre)!=-1):
                str = str[str.index(cutpre) + 9:]
                str = str[:str.index(cutend)]
                if(doi):
                    r.sadd('ref:'+doi, str)
                    r.sadd('refers:'+str,doi)


